package ro.ubb.web.author;

import java.io.IOException;
import java.io.Writer;

import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.ubb.dataaccess.entities.Author;
import ro.ubb.executor.BSException;
import ro.ubb.executor.Executor;

public class GetAuthorByName extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		InitialContext ic;
		try {
			ic = new InitialContext();
			final Executor executor = (Executor) ic.lookup("java:global/bsee-ear/bsee-web-0.0.1-SNAPSHOT/Executor");
			String name = request.getParameter("name");
			Writer out = response.getWriter();

			if (name == null || name.isEmpty()) {
				out.append("ERROR: name parameter null or empty!");
				return;
			}

			out.append("Result:\n");
			try {
				Author author = executor.getAuthorByName(name);
				out.append(author.toString());
			} catch (BSException be) {
				out.append(be.getMessage());
			}

		} catch (NamingException e) {
			e.printStackTrace();
		}
	

	}
}
